//
//  CheckVideoView.swift
//  SBSafetyApp
//
//  Created by Lina on 2022/11/15.
//

import Foundation
import UIKit

class CheckVideoView: ElecCreateScrollView {
    let tjrqTF = DateEffect()
    let val0TF = TextFieldEffect()
    let val1TF = DateEffect()
    let val9TF = LabelPickerEffect(withPlaceholder: "检查状态")   //  检查状态： 正常、 异常
    let val10TF = LabelPickerEffect(withPlaceholder: "是否报修")   //  是否报修： 是、否
    let val2TF = TextFieldEffect()
    let val3TF = DateEffect()
    let val4TF = TextFieldEffect()
    
    let tipsL = UILabel()
    let remindL = UILabel()
    
    var va: Validator?
    
    func registerField(withValidator validator: Validator) {
        if va == nil {
            va = validator
        }
        validator.unregisterAllField()
        
        validator.registerField(tjrqTF.valueL, rules: [RequiredRule(message: tjrqTF.placeholder)])
        validator.registerField(val0TF.valueTF, rules: [RequiredRule(message: val0TF.placeholder)])
        validator.registerField(val1TF.valueL, rules: [RequiredRule(message: val1TF.placeholder)])
        validator.registerField(val9TF.valueL, rules: [RequiredRule(message: val9TF.placeholder)])
        
        if val9TF.value == "异常" {
            validator.registerField(val10TF.valueL, rules: [RequiredRule(message: val10TF.placeholder)])
            validator.registerField(val2TF.valueTF, rules: [RequiredRule(message: val2TF.placeholder)])
            
            if val10TF.value == "是" {
                validator.registerField(val3TF.valueL, rules: [RequiredRule(message: val3TF.placeholder)])
                validator.registerField(val4TF.valueTF, rules: [RequiredRule(message: val4TF.placeholder)])
            }
        }
    }
    
    func checkStatus(withMode mode: String) {
        if mode == "0" {
            self.val10TF.value = nil
            self.val10TF.isHidden = true
            self.val10TF.snp.remakeConstraints { (make) -> Void in
                make.top.equalTo(self.val9TF.snp.bottom)
                make.height.equalTo(0)
            }
            
            self.val2TF.value = nil
            self.val2TF.isHidden = true
            self.val2TF.snp.remakeConstraints { (make) -> Void in
                make.top.equalTo(self.val10TF.snp.bottom)
                make.height.equalTo(0)
            }
            
            self.tipsL.isHidden = true
            self.tipsL.snp.remakeConstraints { (make) -> Void in
                make.top.equalTo(self.val2TF.snp.bottom)
                make.height.equalTo(0)
            }
            
            self.remindL.isHidden = true
            self.remindL.snp.remakeConstraints { (make) -> Void in
                make.top.equalTo(self.tipsL.snp.top)
                make.height.equalTo(0)
            }
            
            self.checkRepair(withMode: "0")
        } else {
            self.val10TF.isHidden = false
            self.val10TF.snp.remakeConstraints { (make) -> Void in
                make.top.equalTo(self.val9TF.snp.bottom).offset(10)
                make.centerX.equalTo(self.snp.centerX)
                make.height.equalTo(self.val0TF.snp.height)
                make.width.equalTo(ScreenWidth - 20)
            }
            
            self.val2TF.isHidden = false
            self.val2TF.snp.remakeConstraints { (make) -> Void in
                make.top.equalTo(self.val10TF.snp.bottom).offset(10)
                make.centerX.equalTo(self.snp.centerX)
                make.height.equalTo(self.val0TF.snp.height)
                make.width.equalTo(ScreenWidth - 20)
            }
            
            self.tipsL.isHidden = false
            self.tipsL.snp.makeConstraints { (make) -> Void in
                make.top.equalTo(self.val2TF.snp.bottom).offset(10)
                make.left.equalTo(self.val2TF.snp.left).offset(10)
                make.width.equalTo(30)
            }
            
            self.remindL.isHidden = false
            self.remindL.snp.makeConstraints { (make) -> Void in
                make.top.equalTo(self.tipsL.snp.top)
                make.left.equalTo(self.tipsL.snp.right)
                make.right.equalTo(self.val2TF.snp.right).offset(-10)
            }
        }
        
        self.registerField(withValidator: self.va!)
    }
    
    func checkRepair(withMode mode: String) {
        if mode == "0" {
            self.val3TF.value = nil
            self.val3TF.isHidden = true
            self.val3TF.snp.remakeConstraints { (make) -> Void in
                make.top.equalTo(self.remindL.snp.bottom)
                make.height.equalTo(0)
            }
            
            self.val4TF.value = nil
            self.val4TF.isHidden = true
            self.val4TF.snp.remakeConstraints { (make) -> Void in
                make.top.equalTo(self.val3TF.snp.bottom)
                make.height.equalTo(0)
            }
            
        } else {
            self.val3TF.isHidden = false
            self.val3TF.snp.remakeConstraints { (make) -> Void in
                make.top.equalTo(self.remindL.snp.bottom).offset(10)
                make.centerX.equalTo(self.snp.centerX)
                make.height.equalTo(self.val0TF.snp.height)
                make.width.equalTo(ScreenWidth - 20)
            }
            
            self.val4TF.isHidden = false
            self.val4TF.snp.remakeConstraints { (make) -> Void in
                make.top.equalTo(self.val3TF.snp.bottom).offset(10)
                make.centerX.equalTo(self.snp.centerX)
                make.height.equalTo(self.val0TF.snp.height)
                make.width.equalTo(ScreenWidth - 20)
            }
        }
        self.registerField(withValidator: self.va!)
    }
    
    override func setupUI() {
        super.setupUI()
        
        titleL.text = "检查数码监控录像情况记录"
        subTitleL.text = "情况记录"
        
        tjrqTF.placeholder = "日期"
        tjrqTF.value = Date.todayDateCN()
        tjrqTF.maximumDate = Date()
        addSubview(tjrqTF)
        tjrqTF.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(subTitleL.snp.bottom).offset(18)
            make.centerX.equalTo(self.snp.centerX)
            make.height.equalTo(56)
            make.width.equalTo(ScreenWidth - 20)
        }
        
        val0TF.placeholder = "机型"
        addSubview(val0TF)
        val0TF.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(tjrqTF.snp.bottom).offset(10)
            make.centerX.equalTo(self.snp.centerX)
            make.height.equalTo(56)
            make.width.equalTo(ScreenWidth - 20)
        }
        
        val1TF.placeholder = "回放日期"
        val1TF.value = Date.todayDateCN()
        val1TF.maximumDate = Date()
        addSubview(val1TF)
        val1TF.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(val0TF.snp.bottom).offset(10)
            make.centerX.equalTo(self.snp.centerX)
            make.height.equalTo(val0TF.snp.height)
            make.width.equalTo(ScreenWidth - 20)
        }
        
        val9TF.dataSource = [
            LabelDictModal(type: "0", dictLabel: "正常", dictValue: "正常"),
            LabelDictModal(type: "1", dictLabel: "异常", dictValue: "异常") // 显示
        ]
        val9TF.didSelectItemWith = { (modal) -> () in
            self.checkStatus(withMode: modal.type ?? "")
        }
        addSubview(val9TF)
        val9TF.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(val1TF.snp.bottom).offset(10)
            make.centerX.equalTo(self.snp.centerX)
            make.height.equalTo(val0TF.snp.height)
            make.width.equalTo(ScreenWidth - 20)
        }
        
        val10TF.dataSource = [
            LabelDictModal(type: "1", dictLabel: "是", dictValue: "是"),
            LabelDictModal(type: "0", dictLabel: "否", dictValue: "否") // 显示
        ]
        val10TF.didSelectItemWith = { (modal) -> () in
            self.checkRepair(withMode: modal.type ?? "")
        }
        val10TF.isHidden = true
        addSubview(val10TF)
        val10TF.snp.makeConstraints { (make) -> Void in
//            make.top.equalTo(val9TF.snp.bottom).offset(10)
//            make.centerX.equalTo(self.snp.centerX)
//            make.height.equalTo(val0TF.snp.height)
//            make.width.equalTo(ScreenWidth - 20)
            make.top.equalTo(val9TF.snp.bottom)
            make.height.equalTo(0)
        }
        
        val2TF.placeholder = "问题描述"
        val2TF.isHidden = true
        addSubview(val2TF)
        val2TF.snp.makeConstraints { (make) -> Void in
//            make.top.equalTo(val10TF.snp.bottom).offset(10)
//            make.centerX.equalTo(self.snp.centerX)
//            make.height.equalTo(val0TF.snp.height)
//            make.width.equalTo(ScreenWidth - 20)
            make.top.equalTo(val10TF.snp.bottom)
            make.height.equalTo(0)
        }
        
        tipsL.font = UIFont.systemFont(ofSize: 14)
        tipsL.textColor = .hex("#E76142")
        tipsL.text = "注："
        tipsL.isHidden = true
        addSubview(tipsL)
        tipsL.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(val2TF.snp.bottom)
            make.height.equalTo(0)
        }
        
        remindL.numberOfLines = 0
        remindL.font = UIFont.systemFont(ofSize: 14)
        remindL.textColor = .hex("#E76142")
        remindL.text = "问题描述指图像是否清晰、稳定，时间是否准确，客户人像和取钞过程是否完整。"
        remindL.isHidden = true
        addSubview(remindL)
        remindL.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(tipsL.snp.top)
            make.height.equalTo(0)
        }
        
        val3TF.placeholder = "报修时间"
        val3TF.isHidden = true
        addSubview(val3TF)
        val3TF.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(remindL.snp.bottom)
            make.height.equalTo(0)
        }
        
        val4TF.placeholder = "报修人"
        val4TF.isHidden = true
        addSubview(val4TF)
        val4TF.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(val3TF.snp.bottom)
            make.height.equalTo(0)
        }
        
        addSubview(submitBtn)
        submitBtn.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(val4TF.snp.bottom).offset(30)
            make.height.equalTo(50)
            make.centerX.equalTo(self.snp.centerX)
            make.width.equalTo(ScreenWidth - 20)
        }
        
        let bottomIV = UIImageView(image: UIImage(named: "elec_bottom"))
        addSubview(bottomIV)
        bottomIV.snp.makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp.centerX)
            make.top.equalTo(submitBtn.snp.bottom).offset(20)
            make.bottom.equalTo(self.snp.bottom).offset(-16)
            make.height.equalTo(24)
        }
    }
}
